package com.sf.appointment.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sf.appointment.vo.AuditVo;
import com.sf.appointment.vo.CarPackageAuditInfo;
import com.sf.appointment.vo.ServiceItemVo;
import com.sf.audit.domain.BusCarPackageAudit;
import com.sf.audit.mapper.BusCarPackageAuditMapper;
import com.sf.common.core.domain.entity.SysUser;
import com.sf.common.utils.DateUtils;
import com.sf.common.utils.SecurityUtils;
import com.sf.management.domain.BusBpmnInfo;
import com.sf.management.mapper.BusBpmnInfoMapper;
import com.sf.system.domain.SysConfig;
import com.sf.system.mapper.SysConfigMapper;
import com.sf.system.mapper.SysRoleMapper;
import com.sf.system.mapper.SysUserMapper;
import com.sf.system.mapper.SysUserRoleMapper;
import io.jsonwebtoken.lang.Assert;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sf.appointment.mapper.BusServiceItemMapper;
import com.sf.appointment.domain.BusServiceItem;
import com.sf.appointment.service.IBusServiceItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 服务项Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-16
 */
@Service
@Transactional
public class BusServiceItemServiceImpl implements IBusServiceItemService
{
    @Autowired
    private BusServiceItemMapper busServiceItemMapper;



    @Autowired
    private SysUserMapper sysUserMapper;


    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Autowired
    private BusBpmnInfoMapper busBpmnInfoMapper;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private BusCarPackageAuditMapper busCarPackageAuditMapper;

    /**
     * 查询服务项
     *
     * @param id 服务项主键
     * @return 服务项
     */
    @Override
    public BusServiceItem selectBusServiceItemById(Long id)
    {
        return busServiceItemMapper.selectBusServiceItemById(id);
    }

    /**
     * 查询服务项列表
     *
     * @param busServiceItem 服务项
     * @return 服务项
     */
    @Override
    public List<BusServiceItem> selectBusServiceItemList(BusServiceItem busServiceItem)
    {
        return busServiceItemMapper.selectBusServiceItemList(busServiceItem);
    }

    /**
     * 新增服务项
     *
     * @param busServiceItem 服务项
     * @return 结果
     */
    @Override
    public int insertBusServiceItem(BusServiceItem busServiceItem)
    {
        busServiceItem.setCreateTime(DateUtils.getNowDate());
        return busServiceItemMapper.insertBusServiceItem(busServiceItem);
    }

    /**
     * 修改服务项
     *
     * @param busServiceItem 服务项
     * @return 结果
     */
    @Override
    public int updateBusServiceItem(ServiceItemVo vo)
    {
        Assert.notNull(vo.getName(),"服务项名称不能为空");
        Assert.notNull(vo.getOriginalPrice(),"原价不能为空");
        Assert.notNull(vo.getDiscountPrice(),"折扣价不能为空");
        Assert.notNull(vo.getCarPackage(),"是否套餐选项不能为空");
        Assert.notNull(vo.getServiceCatalog(),"服务分类不能为空");
        Assert.notNull(vo.getInfo(),"注释不能为空");

        int i = vo.getDiscountPrice().compareTo(vo.getOriginalPrice());

        if(i>0){
            throw new RuntimeException("折扣金额不能大于总金额");
        }
        BusServiceItem busServiceItem=busServiceItemMapper.selectBusServiceItemById(vo.getId());

        if(busServiceItem==null){
            throw new RuntimeException("操作错误");
        }
        if(busServiceItem.getAuditStatus().equals(BusServiceItem.AUDITSTATUS_AUDITING)||busServiceItem.getSaleStatus().equals(BusServiceItem.SALESTATUS_ON)){
            throw new RuntimeException("当前状态无法操作");
        }
        if(busServiceItem.getCarPackage().equals(BusServiceItem.CARPACKAGE_YES)&&busServiceItem.getAuditStatus().equals(BusServiceItem.AUDITSTATUS_APPROVED)){
            busServiceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_INIT);
        }
        if(!(vo.getCarPackage().equals(busServiceItem.getCarPackage()))){
            vo.setCarPackage(busServiceItem.getCarPackage());
        }

        BeanUtils.copyProperties(vo,busServiceItem);

        return busServiceItemMapper.updateBusServiceItem(busServiceItem);
    }

    /**
     * 批量删除服务项
     *
     * @param ids 需要删除的服务项主键
     * @return 结果
     */
    @Override
    public int deleteBusServiceItemByIds(Long[] ids)
    {
        return busServiceItemMapper.deleteBusServiceItemByIds(ids);
    }

    /**
     * 删除服务项信息
     *
     * @param id 服务项主键
     * @return 结果
     */
    @Override
    public int deleteBusServiceItemById(Long id)
    {
        return busServiceItemMapper.deleteBusServiceItemById(id);
    }

    @Override
    public int addBusServiceItemByIds(ServiceItemVo vo) {
        Assert.notNull(vo.getName(),"服务项名称不能为空");
        Assert.notNull(vo.getOriginalPrice(),"原价不能为空");
        Assert.notNull(vo.getDiscountPrice(),"折扣价不能为空");
        Assert.notNull(vo.getCarPackage(),"是否套餐选项不能为空");
        Assert.notNull(vo.getServiceCatalog(),"服务分类不能为空");
        Assert.notNull(vo.getInfo(),"注释不能为空");

        int i = vo.getDiscountPrice().compareTo(vo.getOriginalPrice());

        if(i>0){
            throw new RuntimeException("折扣金额不能大于总金额");
        }
        BusServiceItem busServiceItem = new BusServiceItem();


        if(BusServiceItem.CARPACKAGE_YES.equals(vo.getCarPackage())){
            busServiceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_INIT);
        }else{
            busServiceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        BeanUtils.copyProperties(vo,busServiceItem);
        busServiceItem.setCreateTime(DateUtils.getNowDate());
        return busServiceItemMapper.insertBusServiceItem(busServiceItem);

    }

    @Override
    public int saleOnBusServiceItem(Long id) {
        BusServiceItem busServiceItem = busServiceItemMapper.selectBusServiceItemById(id);
        if(busServiceItem==null){
            throw new RuntimeException("id不合法");
        }
        if(busServiceItem.getSaleStatus().equals(BusServiceItem.SALESTATUS_OFF)){
            if(busServiceItem.getCarPackage().equals(BusServiceItem.CARPACKAGE_YES)){
                if(busServiceItem.getAuditStatus().equals(BusServiceItem.AUDITSTATUS_APPROVED)){
                    busServiceItem.setSaleStatus(BusServiceItem.SALESTATUS_ON);
                }else {
                    throw new RuntimeException("当前状态无法上架");
                }
            }else{
                busServiceItem.setSaleStatus(BusServiceItem.SALESTATUS_ON);
            }
        }else{
            throw new RuntimeException("必须是下架状态才能上架");
        }
        return busServiceItemMapper.updateBusServiceItem(busServiceItem);
    }

    @Override
    public int SaleOffBusServiceItem(Long id) {
        BusServiceItem busServiceItem = busServiceItemMapper.selectBusServiceItemById(id);
        if(busServiceItem==null){
            throw new RuntimeException("id不合法");
        }
        if(busServiceItem.getSaleStatus().equals(BusServiceItem.SALESTATUS_ON)){
                busServiceItem.setSaleStatus(BusServiceItem.SALESTATUS_OFF);
        }else{
            throw new RuntimeException("必须是上架状态才能下架");
        }
        return busServiceItemMapper.updateBusServiceItem(busServiceItem);
    }

    @Override
    public CarPackageAuditInfo audit(Long id) {
        BusServiceItem busServiceItem = busServiceItemMapper.selectBusServiceItemById(id);
        if(busServiceItem==null){
            throw new RuntimeException("id不合法");
        }
        if (BusServiceItem.AUDITSTATUS_INIT.equals(busServiceItem.getAuditStatus())||BusServiceItem.AUDITSTATUS_REPLY.equals(busServiceItem.getAuditStatus())) {
            CarPackageAuditInfo carPackageAuditInfo=new CarPackageAuditInfo();
            carPackageAuditInfo.setServiceItem(busServiceItem);
            List<SysUser> users = sysUserMapper.selectUserByRoleKey("shopowner");
            carPackageAuditInfo.setShopOwners(users);
            SysConfig sysConfig=new SysConfig();
            sysConfig.setConfigKey("discountPrice");
            SysConfig sysConfig1 = sysConfigMapper.selectConfig(sysConfig);
            String configValue = sysConfig1.getConfigValue();
            BigDecimal discountPrice = busServiceItem.getDiscountPrice();
            BigDecimal config=new BigDecimal(configValue);
            carPackageAuditInfo.setDiscountPrice(config);
            if (discountPrice.compareTo(config) >= 0){
                List<SysUser> users1 = sysUserMapper.selectUserByRoleKey("financial");
                carPackageAuditInfo.setFinances(users1);
            }
            return carPackageAuditInfo;
        }else{
            throw new RuntimeException("操作失败");
        }


    }

    @Override
    public int auditVo(AuditVo auditVo) {
        Assert.notNull(auditVo.getShopOwnerId(),"店长信息不能为空");
        Assert.notNull(auditVo.getId(),"id不能为空");
        Assert.notNull(auditVo.getInfo(),"备注不能为空");

        BusServiceItem busServiceItem = busServiceItemMapper.selectBusServiceItemById(auditVo.getId());
        if(busServiceItem==null){
            throw new RuntimeException("id不合法");
        }
        if (BusServiceItem.CARPACKAGE_NO.equals(busServiceItem.getAuditStatus())||BusServiceItem.AUDITSTATUS_REPLY.equals(busServiceItem.getAuditStatus())) {
            BusBpmnInfo bpmnInfo=new BusBpmnInfo();
            bpmnInfo.setBpmnType(0);
            BusBpmnInfo busBpmnInfo = busBpmnInfoMapper.selectBusBpmnInfoByTypeOnlyOne(bpmnInfo);
            BusCarPackageAudit busCarPackageAudit = new BusCarPackageAudit();
            busCarPackageAudit.setServiceItemId(busServiceItem.getId());
            busCarPackageAudit.setServiceItemName(busServiceItem.getName());
            busCarPackageAudit.setServiceItemInfo(busServiceItem.getInfo());
            busCarPackageAudit.setServiceItemPrice(busServiceItem.getDiscountPrice());
            busCarPackageAudit.setCreatorId(SecurityUtils.getUserId().toString());
            busCarPackageAudit.setInfo(auditVo.getInfo());
            busCarPackageAudit.setCreateTime(DateUtils.getNowDate());
            busCarPackageAudit.setStatus(0);

            SysConfig sysConfig=new SysConfig();
            sysConfig.setConfigKey("discountPrice");
            SysConfig sysConfig1 = sysConfigMapper.selectConfig(sysConfig);
            String configValue = sysConfig1.getConfigValue();
            BigDecimal discountPrice = busServiceItem.getDiscountPrice();
            Long discountPrice2=new Long(discountPrice.longValue());
            BigDecimal config=new BigDecimal(configValue);
            Long config2=new Long(config.longValue());
            if (discountPrice2.compareTo(config2) >= 0){
                if(auditVo.getFinanceId()==null){
                    throw new RuntimeException("财务出错");
                }
                busCarPackageAuditMapper.insertBusCarPackageAudit(busCarPackageAudit);
                Map<String,Object> map=new HashMap<>();
                map.put("shopOwnerId",auditVo.getShopOwnerId());
                map.put("disCountPrice",discountPrice2);
                map.put("financeId",auditVo.getFinanceId());
                ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(busBpmnInfo.getProcessDefinitionKey(), busCarPackageAudit.getId().toString(), map);
                String processInstanceId = processInstance.getProcessInstanceId();
                busCarPackageAudit.setInstanceId(processInstanceId);
                busCarPackageAuditMapper.updateBusCarPackageAudit(busCarPackageAudit);

            }else{

                busCarPackageAuditMapper.insertBusCarPackageAudit(busCarPackageAudit);
                Map<String,Object> map=new HashMap<>();
                map.put("shopOwnerId",auditVo.getShopOwnerId());
                map.put("disCountPrice",discountPrice2);
                ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(busBpmnInfo.getProcessDefinitionKey(), busCarPackageAudit.getId().toString(), map);
                String processInstanceId = processInstance.getProcessInstanceId();
                busCarPackageAudit.setInstanceId(processInstanceId);
                busCarPackageAuditMapper.updateBusCarPackageAudit(busCarPackageAudit);
            }
            busServiceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_AUDITING);
            return  busServiceItemMapper.updateBusServiceItem(busServiceItem);


        }else{
            throw new RuntimeException("状态不合法");
        }

    }
}
